<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.componentName"
          clearable
          size="small"
          style="width: 200px"
          placeholder="组件名称"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.relTableName"
          clearable
          size="small"
          style="width: 200px"
          placeholder="真实表名"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.describe"
          clearable
          size="small"
          style="width: 200px"
          placeholder="描述"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rr-operation :crud="crud" />
      </div>
      <crud-operation :permission="permission" />
    </div>
    <!-- 表单渲染 -->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0 && crud.status.add > 0" :title="crud.status.title" width="600px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="组件名称" prop="componentName">
          <el-input v-model="form.componentName" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="真实表名" prop="relTableName">
          <el-input v-model="form.relTableName" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input v-model="form.describe" style="width: 400px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--  修改  -->
    <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0 && crud.status.edit > 0" :title="crud.status.title" width="600px">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item label="组件名称" prop="componentName">
          <el-input v-model="form.componentName" style="width: 400px;" readonly />
        </el-form-item>
        <el-form-item label="真实表名" prop="relTableName">
          <el-input v-model="form.relTableName" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="描述" prop="describe">
          <el-input v-model="form.describe" style="width: 400px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!-- 数据展示 -->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" width="55" />
      <el-table-column v-if="columns.visible('componentName')" prop="componentName" label="组件名称" />
      <el-table-column v-if="columns.visible('relTableName')" prop="relTableName" label="真实表名" />
      <el-table-column v-if="columns.visible('describe')" prop="describe" label="描述" />
      <el-table-column
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button v-permission="['admin','sysSrcTable:conf']" type="warning" icon="el-icon-setting" circle @click="toConfig(scope.row.componentName, scope.row.relTableName)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination />
  </div>
</template>
<script>
import crudSysSrcTable from '@/api/postRecord/sysSrcTable'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import { init } from '@/api/postRecord/sysSrcConfig'

const defaultCrud = CRUD({ title: '源表与菜单映射关系', url: 'api/sysSrcTable', sort: ['componentName,asc'], crudMethod: { ...crudSysSrcTable }})
const defaultForm = { id: null, componentName: null, relTableName: null, describe: null }
export default {
  name: 'SysSrcTable',
  components: {
    crudOperation,
    rrOperation,
    pagination
  },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'sysSrcTable:add'],
        edit: ['admin', 'sysSrcTable:edit'],
        del: ['admin', 'sysSrcTable:del']
      },
      rules: {
        componentName: [
          { required: true, message: '组件名称不能为空', trigger: 'blur' },
          { max: 20, message: '组件名称最大为 20 个字符', trigger: 'blur' }
        ],
        relTableName: [
          { required: true, message: '真实表名不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    toConfig(componentName, relTableName) {
      // console.log(data)
      init(componentName).then(res => {
        // console.log(res)
        this.$router.push('/sysSrcConfig/' + componentName + '/' + relTableName + '/config')
      })
    }
  }
}
</script>
